package com.daylive.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.daylive.model.Reports;
import com.daylive.service.AdminService;
import com.daylive.service.ReportsService;
import com.daylive.util.ObjectExcelView;


@Controller
@RequestMapping("reports")
public class ReportsController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private ReportsService  reportsService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/day_reports.do")
	public ModelAndView getDayReportsList() {
		return getDayReportsList(null);
	}
	@RequestMapping("/day_reports_list.do")
	public ModelAndView getDayReportsList(@RequestParam String time) {
		ModelAndView mv = new ModelAndView("daily_report_list");
		List<Reports> pageList =  reportsService.getRoportsList(time);
		mv.addObject("reportsList", pageList);
		mv.addObject("time", time);
		return mv;
	}
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel.do")
	public ModelAndView exportExcel(){
		ModelAndView mv = new ModelAndView();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("时间");
			titles.add("新增用户");
			titles.add("新增直播");
			titles.add("参与者");
			titles.add("直播时间");	 
			dataMap.put("titles", titles);
			final List<Reports> inputList = reportsService.getRoportsList();
			List<HashMap> outList = new ArrayList<HashMap>();
			HashMap map = null;
			for(int i=0;i<inputList.size();i++){
				map = new HashMap();
				map.put("var1", inputList.get(i).getTime());	 
				map.put("var2", inputList.get(i).getNewUsers());
				map.put("var3", inputList.get(i).getLives());
				map.put("var4", inputList.get(i).getParticipants());
				map.put("var5", inputList.get(i).getMinutes());
				outList.add(map);
			}
			dataMap.put("outList", outList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			e.printStackTrace();
		}
		return mv;
	}
}